// 设备组
<template>
  <div class="item1">
    <div class="content">
      <div class="left">
        <div class="title"> {{$t('basic.equip_group')}} </div>
        <el-form ref="form" :model="formData" label-width="0" size="mini">
          <el-row :gutter="28">
            <el-col :xs="23" :sm="23" :md="23" :lg="23" :xl="23" style="padding-top:10px">
              <el-form-item label-position="left" style="margin-bottom:5px">
                <el-input v-model="formData.keyword" clearable placeholder="" class="search">
                  <template slot="append">
                    <el-button type="primary" size="small" @click="handleSearch">{{ $t("common.query") }}</el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="btngroup">
          <el-button type="primary" size="mini" @click="handleAdd()">{{ $t("common.add") }}</el-button>
          <el-button type="primary" size="mini" @click="handleEdit()">{{ $t("common.edit") }}</el-button>
          <el-button type="primary" size="mini" @click="handleDel()">{{ $t("common.remove") }}</el-button>
          <!-- <el-button type="primary" size="mini" @click="handleAdddevice()">添加设备</el-button> -->
        </div>

        <div class="tableAuto">
          <el-table :data="tableData" :height="height" style="width:100%" stripe :header-cell-style="{background:'#EAEBEC'}"
            v-loading="tableLoading" @row-click="rowClick" ref="refTable" highlight-current-row>
            <el-table-column label="" width="50" align="center">
              <template slot-scope="scope">
                <div>
                  <el-radio :label="scope.row.fid" v-model="radioId">&nbsp;</el-radio>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="entName" width="" show-overflow-tooltip label=""></el-table-column>
          </el-table>
        </div>
        <div class="pagination-area">
          <el-pagination @current-change="handleCurrentChange" :page-size="pager.pageSize" :current-page="pager.pageNum"
            layout="prev, pager, next" :total="pager.total">
          </el-pagination>
        </div>
      </div>
      <div class="right">
        <div class="title">{{$t('basic.include_equip')}}</div>
        <subDevice :fid="radioId" :description="description" v-if="radioId"></subDevice>
      </div>
    </div>

    <el-dialog v-dialogDrag :title="title" :visible.sync="isShowDialog" :close-on-click-modal="false"
      :width="screenWidth > 1024 ? '30%' : '50%'" class="isShowDialog" :before-close="handleClose">
      <el-form :model="ruleForm" label-width="85px" ref="ruleForm" size="small" :rules="rules" :hide-required-asterisk="true">
        <el-row :gutter="35">
          <el-col :xs="24">
            <el-form-item :label="$t('basic.equip_group_name')" prop="entName">
              <el-input v-model="ruleForm.entName" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('common.describe')" prop="description">
              <el-input v-model="ruleForm.description" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleCreateMaterial('ruleForm')"
          :loading="loading">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { configure } from "@/api/configure"
import subDevice from "./subDevice.vue"
export default {
  components: {
    subDevice
  },
  data () {
    return {
      height: document.body.offsetHeight - 335,
      screenWidth: document.body.clientWidth,
      formData: {
        keyword: ""
      },
      icon: "icon-biaoge",
      tableData: [],
      pager: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      tableLoading: false,
      radioId: "",
      ruleForm: {
        entName: "",
        description: "",

      },
      rules: {
        entName: [{ required: true, message: '设备组名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
      },
      loading: false,
      title: "新增物料信息",
      isShowDialog: false,
      description: "",
      currObj: {}
    }
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    }
  },
  mounted () {
    const that = this
    window.addEventListener('resize', function () {
      that.screenWidth = document.body.offsetWidth;
    })
  },
  created () {
    this.getTrainList()
  },
  methods: {
    handleSearch () {
      this.pager.pageNum = 1;
      this.getTrainList()
    },
    getTrainList () {
      const param = {
        entName: this.formData.keyword,
        page: this.pager.pageNum,
        size: this.pager.pageSize
      }
      this.tableLoading = true
      configure.getTrainList(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.tableData = res.data.rows
          if (this.tableData.length > 0) {
            this.radioId = this.tableData[0].fid
            this.description = this.tableData[0].description
            this.currObj = this.tableData[0]

          }
          this.pager.total = res.data.total
        }
      })
    },
    handleCurrentChange (val) {
      this.pager.pageNum = val;
      this.getTrainList()
    },
    rowClick (row) {
      this.radioId = row.fid;
      this.description = row.description
      this.currObj = row
      this.$refs.refTable.setCurrentRow(row);
    },
    handleAdd () {
      this.title = this.$t('basic.add_equip_group')
      this.ruleForm = {}
      try {
        this.$refs['ruleForm'].resetFields();
      } catch (e) { }
      this.isShowDialog = true
    },
    async handleEdit () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_edit'));
        return
      }
      this.title = this.$t('basic.edit_equip_group')
      try {
        let res = await configure.findTrainDetali(this.radioId)
        if (res.code == 200) {
          this.ruleForm = {
            entName: res.data.entName,
            description: res.data.description,
            fid: res.data.fid,
          }
          this.isShowDialog = true
        }
      } catch (e) {

      }

    },
    async handleDel () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_del'));
        return
      }
      try {
        let res = await configure.findTrainDetali(this.radioId)
        let that = this
        if (res.code == 200) {
          this.$customConfirm({
            content: "请确认是否删除设备组" + this.currObj.entName,
            onok () {
              configure.deleteTrain(res.data.fid).then(res1 => {
                if (res1.code == 200) {
                  that.radioId = ""
                  that.cardIndex = -1
                  that.getTrainList()
                  that.$message({ duration: 1000, type: 'success', message: this.$t('common.del_succ') });
                } else {
                  that.$message.error(res1.msg);
                }
              })
            }
          });
        } else {
          this.$message.error(res.msg);
        }
      } catch (e) {

      }
    },
    handleCreateMaterial (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.ruleForm)
          this.loading = true
          configure.createTrainList(param, this.title).then(res => {
            this.loading = false
            if (res.code == 200) {
              this.$refs[formName].resetFields();
              this.getTrainList()
              this.isShowDialog = false
            } else {
              this.$message.error(res.msg);
            }
          }).catch(e => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    handleClose () {
      this.isShowDialog = false
      this.$refs['ruleForm'].resetFields();
    },
  }
}
</script>

<style scoped>
.change >>> .iconfont {
  font-size: 13px !important;
}
.titmepicker >>> .el-date-editor {
  width: 100%;
}
.titmepicker >>> .el-date-editor .el-range-separator {
  width: 8%;
}
.isShowDialog >>> .el-select {
  width: 100%;
}
.tableAuto,
.cardBox {
  clear: both;
}
.pagination-area {
  padding-top: 0px;
}
.Desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cardBox {
  overflow-y: auto;
}
.card {
  background: #eaebec;
  margin-bottom: 20px;
  padding: 10px;
  border-radius: 3px;
}
.card p {
  font-size: 12px;
  line-height: 22px;
  transform: scale(0.9);
}
.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.title {
  font-weight: bold;
}
.selectCard {
  background: #f6fbff;
}
@media only screen and (min-width: 1200px) {
  .el-col-lg-5 {
    width: 19.83333%;
  }
}
.content {
  display: flex;
  height: 100%;
  padding-top: 10px;
  clear: both;
}
.content .left {
  width: 200px;
  border-right: 1px solid #e4e4e4;
  height: 100%;
}
.content .right {
  flex: 1;
  padding-left: 15px;
}
.tableAuto >>> .el-table__row {
  background: none !important;
}
.tableAuto >>> .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: none;
}
.tableAuto >>> .el-table__row td {
  padding: 8px 0;
}
.tableAuto >>> .el-table thead {
  display: none;
}
.btngroup >>> .el-button--mini {
  padding: 7px 12px;
  font-size: 12px;
  /* transform:scale(0.8);
        margin: 0; */
}
.search >>> .el-input-group__append,
.search >>> .el-input-group__prepend {
  background: #00a09d;
  color: #fff;
}
.title {
  line-height: 35px;
  background: #3b2e58;
  color: #fff;
  padding: 0 15px;
  font-size: 14px;
  cursor: pointer;
}
</style>